<template>
  <div class="dept-tree-container">
    <!-- 搜索区域 -->
    <div class="search-container" v-if="showSearch">
      <el-input
        v-model="deptName"
        class="enhanced-search-input"
        clearable
        placeholder="请输入部门名称"
      >
        <template #prefix>
          <Icon icon="ep:search" class="search-icon" />
        </template>
      </el-input>
    </div>

    <!-- 树形结构区域 -->
    <div class="tree-container">
      <el-tree
        ref="treeRef"
        :data="deptList"
        :expand-on-click-node="false"
        :filter-node-method="filterNode"
        :props="defaultProps"
        default-expand-all
        highlight-current
        node-key="id"
        :show-checkbox="showCheckbox"
        @node-click="handleNodeClick"
        @check="currentChange"
        :default-checked-keys="checkedKeys"
        class="enhanced-tree"
      />
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ElTree } from 'element-plus'
import * as DeptApi from '@/api/system/dept'
import { defaultProps, handleTree } from '@/utils/tree'

defineOptions({ name: 'SystemUserDeptTree' })
const props = defineProps({
  showCheckbox: {
    type: Boolean,
    default: () => false
  },
  showSearch: {
    type: Boolean,
    default: () => true
  }
})
// 默认选择的值
const checkedKeys = ref([])
const deptName = ref('')
const deptList = ref<Tree[]>([]) // 树形结构
const treeRef = ref<InstanceType<typeof ElTree>>()

// 选择事件
const currentChange = (checkNodes, checkedKeys) => {
  emits('currentChange', checkNodes, checkedKeys)
}
// 回显选择的值
const setCheckedKeys = (val) => {
  if (val.length) {
    checkedKeys.value = val
  } else {
    treeRef.value.setCheckedKeys([])
  }
}
/** 获得部门树 */
const getTree = async () => {
  const res = await DeptApi.getSimpleDeptList()
  deptList.value = []
  deptList.value.push(...handleTree(res))
}

/** 基于名字过滤 */
const filterNode = (name: string, data: Tree) => {
  if (!name) return true
  return data.name.includes(name)
}

/** 处理部门被点击 */
const handleNodeClick = async (row: { [key: string]: any }) => {
  emits('node-click', row)
}
const emits = defineEmits(['node-click', 'currentChange'])

/** 监听deptName */
watch(deptName, (val) => {
  treeRef.value!.filter(val)
})

/** 初始化 */
onMounted(async () => {
  await getTree()
})
defineExpose({ setCheckedKeys })
</script>

<style lang="scss" scoped>
// 主题色变量
$primary-color: #0052cc;
$primary-light: rgba(0, 82, 204, 0.1);
$primary-border: rgba(0, 82, 204, 0.3);
$success-color: #67c23a;
$warning-color: #e6a23c;
$danger-color: #f56c6c;
$info-color: #909399;
$text-primary: #303133;
$text-regular: #606266;
$text-secondary: #909399;
$text-placeholder: #c0c4cc;
$border-color: #dcdfe6;
$border-light: #e4e7ed;
$bg-color: #f5f7fa;
$shadow-light: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
$shadow-base:
  0 2px 4px rgba(0, 0, 0, 0.12),
  0 0 6px rgba(0, 0, 0, 0.04);
$border-radius: 12px;
$border-radius-small: 8px;

.dept-tree-container {
  height: 100%;
  display: flex;
  flex-direction: column;
  background: white;
  border-radius: $border-radius-small;
  overflow: hidden;

  @media (max-width: 768px) {
    border-radius: 0;
  }
}

/* 搜索区域样式 */
.search-container {
  padding: 16px;
  background: linear-gradient(135deg, #f8f9ff 0%, #f0f2ff 100%);
  border-bottom: 1px solid $border-light;
  flex-shrink: 0;

  @media (max-width: 768px) {
    padding: 12px;
  }
}

.enhanced-search-input {
  :deep(.el-input__wrapper) {
    border-radius: $border-radius-small;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    transition: all 0.2s ease;
    padding: 8px 12px;
    border: 1px solid $border-light;

    &:hover {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.1);
      border-color: $primary-border;
    }

    &.is-focus {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.2);
      border-color: $primary-color;
    }
  }

  :deep(.el-input__inner) {
    font-size: 14px;
    color: $text-regular;

    &::placeholder {
      color: $text-placeholder;
      font-size: 13px;
    }

    @media (max-width: 768px) {
      font-size: 13px;

      &::placeholder {
        font-size: 12px;
      }
    }
  }
}

.search-icon {
  color: $primary-color;
  font-size: 16px;
  transition: all 0.3s ease;

  @media (max-width: 768px) {
    font-size: 14px;
  }
}

/* 树形结构区域 */
.tree-container {
  flex: 1;
  padding: 16px;
  overflow-y: auto;
  background: white;

  @media (max-width: 768px) {
    padding: 12px;
  }

  /* 滚动条美化 */
  &::-webkit-scrollbar {
    width: 6px;
  }

  &::-webkit-scrollbar-track {
    background: rgba(0, 0, 0, 0.05);
    border-radius: 3px;
  }

  &::-webkit-scrollbar-thumb {
    background: rgba(0, 82, 204, 0.3);
    border-radius: 3px;
    transition: all 0.3s ease;

    &:hover {
      background: rgba(0, 82, 204, 0.5);
    }
  }
}

/* 树形组件样式 */
.enhanced-tree {
  :deep(.el-tree-node) {
    margin-bottom: 4px;

    &:last-child {
      margin-bottom: 0;
    }

    .el-tree-node__content {
      height: 40px;
      border-radius: $border-radius-small;
      transition: all 0.3s ease;
      padding: 0 8px;
      margin-bottom: 2px;
      border: 1px solid transparent;

      &:hover {
        background: $primary-light;
        border-color: $primary-border;
        transform: translateX(2px);
      }

      &.is-current {
        background: linear-gradient(135deg, $primary-color 0%, #1d4ed8 100%);
        color: white;
        border-color: $primary-color;
        box-shadow: 0 2px 8px rgba(0, 82, 204, 0.3);

        .el-tree-node__expand-icon {
          color: white;
        }

        .el-tree-node__label {
          color: white;
          font-weight: 600;
        }
      }

      @media (max-width: 768px) {
        height: 36px;
        padding: 0 6px;
      }
    }

    .el-tree-node__expand-icon {
      color: $primary-color;
      font-size: 16px;
      transition: all 0.3s ease;
      padding: 4px;
      border-radius: 4px;

      &:hover {
        background: $primary-light;
      }

      &.is-leaf {
        color: transparent;
      }

      @media (max-width: 768px) {
        font-size: 14px;
        padding: 3px;
      }
    }

    .el-tree-node__label {
      font-size: 14px;
      color: $text-regular;
      font-weight: 500;
      transition: all 0.3s ease;
      padding: 4px 8px;
      border-radius: 4px;
      flex: 1;
      display: flex;
      align-items: center;
      gap: 8px;

      @media (max-width: 768px) {
        font-size: 13px;
        padding: 3px 6px;

        &::before {
          width: 5px;
          height: 5px;
        }
      }
    }

    .el-checkbox {
      margin-right: 8px;

      :deep(.el-checkbox__input) {
        .el-checkbox__inner {
          border-color: $primary-border;
          border-radius: 4px;
          transition: all 0.3s ease;

          &:hover {
            border-color: $primary-color;
          }
        }

        &.is-checked {
          .el-checkbox__inner {
            background-color: $primary-color;
            border-color: $primary-color;
          }
        }

        &.is-indeterminate {
          .el-checkbox__inner {
            background-color: $primary-color;
            border-color: $primary-color;
          }
        }
      }

      @media (max-width: 768px) {
        margin-right: 6px;

        :deep(.el-checkbox__input) {
          .el-checkbox__inner {
            width: 16px;
            height: 16px;

            &::after {
              width: 4px;
              height: 8px;
            }
          }
        }
      }
    }
  }

  // 子节点缩进样式
  :deep(.el-tree-node__children) {
    .el-tree-node {
      .el-tree-node__content {
        position: relative;

        &::before {
          content: '';
          position: absolute;
          left: -10px;
          top: 50%;
          width: 8px;
          height: 1px;
          background: $primary-border;
        }

        @media (max-width: 768px) {
          margin-left: 16px;

          &::before {
            left: -8px;
            width: 6px;
          }
        }
      }
    }
  }

  // 空状态样式
  :deep(.el-tree__empty-block) {
    padding: 40px 0;

    .el-tree__empty-text {
      color: $text-secondary;
      font-size: 14px;
      position: relative;

      &::before {
        content: '🏢';
        display: block;
        font-size: 32px;
        margin-bottom: 8px;
      }

      @media (max-width: 768px) {
        font-size: 13px;

        &::before {
          font-size: 28px;
          margin-bottom: 6px;
        }
      }
    }
  }
}

/* 深色模式适配 */
@media (prefers-color-scheme: dark) {
  .dept-tree-container {
    background: #374151;
  }

  .search-container {
    background: linear-gradient(135deg, #4b5563 0%, #374151 100%);
    border-bottom-color: #6b7280;
  }

  .tree-container {
    background: #374151;
  }

  .enhanced-search-input {
    :deep(.el-input__wrapper) {
      background: #4b5563;
      border-color: #6b7280;

      &:hover {
        border-color: rgba(96, 165, 250, 0.3);
      }

      &.is-focus {
        border-color: #60a5fa;
      }
    }

    :deep(.el-input__inner) {
      color: #f9fafb;

      &::placeholder {
        color: #9ca3af;
      }
    }
  }

  .search-icon {
    color: #60a5fa;
  }

  .enhanced-tree {
    :deep(.el-tree-node) {
      .el-tree-node__content {
        &:hover {
          background: rgba(96, 165, 250, 0.1);
          border-color: rgba(96, 165, 250, 0.3);
        }

        &.is-current {
          background: linear-gradient(135deg, #60a5fa 0%, #3b82f6 100%);
          border-color: #60a5fa;
        }
      }

      .el-tree-node__expand-icon {
        color: #60a5fa;

        &:hover {
          background: rgba(96, 165, 250, 0.1);
        }
      }

      .el-tree-node__label {
        color: #f9fafb;

        &::before {
          background: #60a5fa;
        }
      }

      .el-checkbox {
        :deep(.el-checkbox__input) {
          .el-checkbox__inner {
            border-color: rgba(96, 165, 250, 0.3);
            background: #4b5563;

            &:hover {
              border-color: #60a5fa;
            }
          }

          &.is-checked {
            .el-checkbox__inner {
              background-color: #60a5fa;
              border-color: #60a5fa;
            }
          }

          &.is-indeterminate {
            .el-checkbox__inner {
              background-color: #60a5fa;
              border-color: #60a5fa;
            }
          }
        }
      }
    }

    :deep(.el-tree-node__children) {
      .el-tree-node {
        .el-tree-node__content {
          &::before {
            background: rgba(96, 165, 250, 0.3);
          }
        }
      }
    }

    :deep(.el-tree__empty-block) {
      .el-tree__empty-text {
        color: #9ca3af;
      }
    }
  }
}

/* 响应式优化 */
@media (max-width: 480px) {
  .search-container {
    padding: 10px;
  }

  .tree-container {
    padding: 10px;
  }

  .enhanced-tree {
    :deep(.el-tree-node) {
      .el-tree-node__content {
        height: 32px;
        padding: 0 4px;
      }

      .el-tree-node__expand-icon {
        font-size: 12px;
        padding: 2px;
      }

      .el-tree-node__label {
        font-size: 12px;
        padding: 2px 4px;

        &::before {
          width: 4px;
          height: 4px;
        }
      }

      .el-checkbox {
        margin-right: 4px;

        :deep(.el-checkbox__input) {
          .el-checkbox__inner {
            width: 14px;
            height: 14px;

            &::after {
              width: 3px;
              height: 6px;
            }
          }
        }
      }
    }

    :deep(.el-tree-node__children) {
      .el-tree-node {
        .el-tree-node__content {
          margin-left: 12px;

          &::before {
            left: -6px;
            width: 4px;
          }
        }
      }
    }

    :deep(.el-tree__empty-block) {
      padding: 30px 0;

      .el-tree__empty-text {
        font-size: 12px;

        &::before {
          font-size: 24px;
          margin-bottom: 4px;
        }
      }
    }
  }
}
</style>
